﻿using System;
using System.Collections.Generic;

using DigitalQuran.Data.Metadata;

namespace DigitalQuran.Collection
{
    /// <summary>
    /// Collection of <see cref="DigitalQuran.Quarter"/>
    /// </summary>
    public class QuarterCollection : ReadOnlyCollection<Quarter>
    {
        public const int TotalQuarters = 240;

        internal QuarterCollection() : this(0, TotalQuarters) { }

        int startIndex = 0;
        public QuarterCollection(int startIndex, int count)
            : base(count)
        {
            this.startIndex = startIndex;
        }

        public override Quarter this[int index]
        {
            get
            {
                if (index < Count)
                    return _Quarter[index + startIndex];
                else
                    throw new IndexOutOfRangeException();
            }
        }

        public override IEnumerator<Quarter> GetEnumerator()
        {
            for (int i = 0; i < Count; i++)
            {
                yield return _Quarter[i + startIndex];
            }
        }

        static Quarter[] _Quarter = new Quarter[TotalQuarters];
        internal static void LoadData(IEnumerator<ValueSpan> e)
        {
            for (int i = 0; i < TotalQuarters; i++)
            {
                e.MoveNext();
                _Quarter[i] = new Quarter(i, e.Current.LowerBound, e.Current.Count);
            }
        }
    }
}